package com.object.module.cbb.service.impl;

import com.object.configuration.utils.IdsUtil;
import com.object.configuration.utils.Q;
import com.object.module.cbb.entity.CddShopEntity;
import com.object.module.cbb.service.CddShopService;
import com.object.module.cbb.service.CddSpikeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class cddSpikeServiceimpl implements CddSpikeService {
    @Autowired
    private CddShopService cddShopService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Value("${spike.shop.name}")
    private String spikeName;
    @Value("${spike.shop.start.name}")
    private String SpikeStartName;

    /**
     * 查询秒杀商品
     *
     * @return
     */
    @Override
    public Q list() {
        String lids = (String) redisTemplate.opsForValue().get(spikeName);
        if (StringUtils.isEmpty(lids)) {
            return Q.error().message("职无秒杀商品");
        }
        List<Integer> ids = IdsUtil.changeId(lids);
        List<CddShopEntity> list = cddShopService.listByIds(ids);
        for (CddShopEntity entity : list) {
            entity.setSpikeCount((Integer) redisTemplate.opsForValue().get(SpikeStartName + entity.getSpId()));
        }
        return Q.ok().put("data", list);
    }
}
